package org.example.y24.m12;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class Text {


    public static void main(String[] args) {
        // 假设有一个Courses类，其中包含获取课程的方法
        Courses courses = new Courses();
        LocalDate today = LocalDate.now();
        long col = today.getDayOfWeek().getValue(); // 获取今天是一周中的第几天
        long week = calculateWeekOfYear(today); // 计算当前是第几周
        Course[] courseList = new Course[5];//今天的课程
        int day = (int) (col - 1);

        // 输出今天的全部课程，并获取今天的全部课程
        getNowCourse(courseList, day);
        //获取时间信息
        Time time = new Time();
        LocalTime[] segmentStarts = Time.getSegmentStarts();//上课时间表
        LocalTime currentTime = LocalTime.now();//当前时间
        int segmentIndex = determineCurrentSegmentIndex(currentTime, segmentStarts);//当前时间段开始时间
        //获取下节课信息
        int nextCourseIndex = courseInformation(courseList, segmentIndex);
        if (nextCourseIndex == 5) {
            System.out.println("今天已经没有课了");
        } else {
            // 下节课上课时间：时间表下标（nextCourseIndex * 2 + 1）；
            // 当前时间到下节课的时间
            LocalTime nextClassStart = segmentStarts[nextCourseIndex * 2 + 1];
            long minutesUntilNextClass = ChronoUnit.MINUTES.between(currentTime, nextClassStart);
            System.out.println("下一节课程 " + courseList[nextCourseIndex].getName() + " 将在 " + nextClassStart + " 开始，还有 " + minutesUntilNextClass + " 分钟。");
        }
    }

    private static int courseInformation(Course[] courseList, int segmentIndex) {
        int nowCouseIndex = segmentIndex/2;
        if (nowCouseIndex < Courses.COURSE_NUMBER && courseList[nowCouseIndex] != null) {
            System.out.println("当前课程是：" + courseList[nowCouseIndex].getName());
        }else {
            System.out.println("当前时间没有课");
        }
        for (int i = nowCouseIndex+1; i < Courses.COURSE_NUMBER; i++) {
            if (courseList[i] != null) {
                System.out.println("下节课是第" + (i + 1) + "节课:" + courseList[i].getName());
                return i;
            }
        }
        return Courses.COURSE_NUMBER;
    }


    private static boolean isCourseActive(Course course, long currentWeek) {
        return currentWeek >= course.getStartWeek() &&
                currentWeek <= course.getEndWeek() &&
                (course.getWeek() == 0 ||
                        (course.getWeek() == 1 && currentWeek % 2 == 0) ||
                        (course.getWeek() == 2 && currentWeek % 2 == 1));
    }

    private static int determineCurrentSegmentIndex(LocalTime currentTime, LocalTime[] segmentStarts) {
        int segmentIndex = -1;
        for (int i = 0; i < segmentStarts.length; i++) {
            LocalTime nextSegmentStart = (i + 1 < segmentStarts.length) ? segmentStarts[i + 1] : LocalTime.MIDNIGHT;
            if (currentTime.isAfter(segmentStarts[i]) && currentTime.isBefore(nextSegmentStart)) {
                segmentIndex = i;
                break;
            }
        }
        return segmentIndex == -1 ? 11 : segmentIndex;
    }


    /**
     * 获取今天全部课程信息
     * @param courseList
     * @param day
     */
    private static void getNowCourse(Course[] courseList, int day) {
        for (int i = 0; i < Courses.COURSE_NUMBER; i++) {
            Course course = Courses.getCourse(day, i);
            if (course != null) {
                courseList[i] = course;
                System.out.println(course.getName());
            }else {
                courseList[i] = null;
                System.out.println("null");
            }
        }
    }
    /**
     * 计算当前是第几周
     */
    private static long calculateWeekOfYear(LocalDate date) {
        LocalDate startOfYear = LocalDate.of(date.getYear(), 1, 1);
        return (date.toEpochDay() - startOfYear.toEpochDay()) / 7 + 1;
    }
}